package com.scheduling.inventory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scheduling.common.result.Result;
import com.scheduling.inventory.dto.ProductInventoryDTO;
import com.scheduling.inventory.dto.ProductInventoryQueryDTO;
import com.scheduling.inventory.service.ProductInventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 产品库存Controller
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@Tag(name = "产品库存管理", description = "产品库存的增删改查、在制品管理、完成品管理等功能")
@RestController
@RequestMapping("/api/inventory/product")
@RequiredArgsConstructor
@Validated
public class ProductInventoryController {

    private final ProductInventoryService productInventoryService;

    @Operation(summary = "分页查询产品库存", description = "支持按产品名称、库存数量等条件查询")
    @PostMapping("/page")
    public Result<IPage<ProductInventoryDTO>> getInventoryPage(@RequestBody ProductInventoryQueryDTO queryDTO) {
        log.info("分页查询产品库存，参数：{}", queryDTO);
        IPage<ProductInventoryDTO> page = productInventoryService.getInventoryPage(queryDTO);
        return Result.success(page);
    }

    @Operation(summary = "根据库存ID查询库存详情")
    @GetMapping("/{inventoryId}")
    public Result<ProductInventoryDTO> getInventoryById(
            @Parameter(description = "库存ID") @PathVariable String inventoryId) {
        log.info("查询库存详情，库存ID：{}", inventoryId);
        ProductInventoryDTO inventory = productInventoryService.getInventoryById(inventoryId);
        return Result.success(inventory);
    }

    @Operation(summary = "根据产品ID查询库存信息")
    @GetMapping("/product/{productId}")
    public Result<ProductInventoryDTO> getInventoryByProductId(
            @Parameter(description = "产品ID") @PathVariable String productId) {
        log.info("根据产品ID查询库存，产品ID：{}", productId);
        ProductInventoryDTO inventory = productInventoryService.getInventoryByProductId(productId);
        return Result.success(inventory);
    }

    @Operation(summary = "新增产品库存")
    @PostMapping
    public Result<Boolean> createInventory(@Validated @RequestBody ProductInventoryDTO inventoryDTO) {
        log.info("新增产品库存，参数：{}", inventoryDTO);
        boolean result = productInventoryService.createInventory(inventoryDTO);
        return Result.success(result, "库存新增成功");
    }

    @Operation(summary = "更新产品库存")
    @PutMapping
    public Result<Boolean> updateInventory(@Validated @RequestBody ProductInventoryDTO inventoryDTO) {
        log.info("更新产品库存，参数：{}", inventoryDTO);
        boolean result = productInventoryService.updateInventory(inventoryDTO);
        return Result.success(result, "库存更新成功");
    }

    @Operation(summary = "删除产品库存")
    @DeleteMapping("/{inventoryId}")
    public Result<Boolean> deleteInventory(
            @Parameter(description = "库存ID") @PathVariable String inventoryId) {
        log.info("删除产品库存，库存ID：{}", inventoryId);
        boolean result = productInventoryService.deleteInventory(inventoryId);
        return Result.success(result, "库存删除成功");
    }

    @Operation(summary = "批量删除产品库存")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteInventory(@RequestBody List<String> inventoryIds) {
        log.info("批量删除产品库存，数量：{}", inventoryIds.size());
        boolean result = productInventoryService.batchDeleteInventory(inventoryIds);
        return Result.success(result, "批量删除成功");
    }

    @Operation(summary = "投入生产", description = "增加在制品数量")
    @PutMapping("/add-wip/{productId}")
    public Result<Boolean> addToWip(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "投入数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "投入原因") @RequestParam String reason) {
        log.info("投入生产，产品ID：{}，数量：{}，原因：{}", productId, quantity, reason);
        boolean result = productInventoryService.addToWip(productId, quantity, reason);
        return Result.success(result, "投入生产成功");
    }

    @Operation(summary = "完成生产", description = "从在制品转为完成品")
    @PutMapping("/complete/{productId}")
    public Result<Boolean> completeProduction(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "完成数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "完成原因") @RequestParam String reason) {
        log.info("完成生产，产品ID：{}，数量：{}，原因：{}", productId, quantity, reason);
        boolean result = productInventoryService.completeProduction(productId, quantity, reason);
        return Result.success(result, "完成生产成功");
    }

    @Operation(summary = "产品出库", description = "减少完成品数量")
    @PutMapping("/ship/{productId}")
    public Result<Boolean> shipProduct(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "出库数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "出库原因") @RequestParam String reason) {
        log.info("产品出库，产品ID：{}，数量：{}，原因：{}", productId, quantity, reason);
        boolean result = productInventoryService.shipProduct(productId, quantity, reason);
        return Result.success(result, "产品出库成功");
    }

    @Operation(summary = "增加在制品数量")
    @PutMapping("/wip/increase/{productId}")
    public Result<Boolean> increaseWipQuantity(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "增加数量") @RequestParam BigDecimal amount) {
        log.info("增加在制品数量，产品ID：{}，数量：{}", productId, amount);
        boolean result = productInventoryService.addToWip(productId, amount, "手动增加在制品数量");
        return Result.success(result, "在制品数量增加成功");
    }

    @Operation(summary = "减少在制品数量")
    @PutMapping("/wip/decrease/{productId}")
    public Result<Boolean> decreaseWipQuantity(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "减少数量") @RequestParam BigDecimal amount) {
        log.info("减少在制品数量，产品ID：{}，数量：{}", productId, amount);
        boolean result = productInventoryService.decreaseWipQuantity(productId, amount, "手动减少在制品数量");
        return Result.success(result, "在制品数量减少成功");
    }

    @Operation(summary = "增加完成品数量")
    @PutMapping("/finished/increase/{productId}")
    public Result<Boolean> increaseFinishedQuantity(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "增加数量") @RequestParam BigDecimal amount) {
        log.info("增加完成品数量，产品ID：{}，数量：{}", productId, amount);
        boolean result = productInventoryService.increaseFinishedQuantity(productId, amount, "手动增加完成品数量");
        return Result.success(result, "完成品数量增加成功");
    }

    @Operation(summary = "减少完成品数量")
    @PutMapping("/finished/decrease/{productId}")
    public Result<Boolean> decreaseFinishedQuantity(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "减少数量") @RequestParam BigDecimal amount) {
        log.info("减少完成品数量，产品ID：{}，数量：{}", productId, amount);
        boolean result = productInventoryService.decreaseFinishedQuantity(productId, amount, "手动减少完成品数量");
        return Result.success(result, "完成品数量减少成功");
    }

    @Operation(summary = "检查完成品库存是否充足")
    @GetMapping("/check-finished/{productId}")
    public Result<Boolean> checkFinishedInventorySufficient(
            @Parameter(description = "产品ID") @PathVariable String productId,
            @Parameter(description = "所需数量") @RequestParam BigDecimal requiredQuantity) {
        log.info("检查完成品库存充足性，产品ID：{}，所需数量：{}", productId, requiredQuantity);
        boolean sufficient = productInventoryService.checkFinishedInventorySufficient(productId, requiredQuantity);
        return Result.success(sufficient, sufficient ? "库存充足" : "库存不足");
    }

    @Operation(summary = "获取库存汇总信息")
    @GetMapping("/summary")
    public Result<List<ProductInventoryDTO>> getInventorySummary() {
        log.info("获取库存汇总信息");
        List<ProductInventoryDTO> summaryList = productInventoryService.getInventorySummary();
        return Result.success(summaryList);
    }
} 